package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.vo.DispensePatientVo;
import com.ncst.backend.domain.vo.DispenseItemVo;
import com.ncst.backend.service.DispenseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dispense")
@RequiredArgsConstructor
public class DispenseController {

    private final DispenseService dispenseService;

    @GetMapping("/patient/{caseNumber}")
    public Result<DispensePatientVo> getPatient(@PathVariable String caseNumber) {
        DispensePatientVo vo = dispenseService.getPatientByCaseNumber(caseNumber);
        return vo == null ? Result.notFound("未查到患者") : Result.success(vo);
    }


    @GetMapping("/consume/{caseNumber}")
    public Result<List<DispenseItemVo>> getUnDispense(@PathVariable String caseNumber) {
        List<DispenseItemVo> list = dispenseService.listWaitDispense(caseNumber);
        return Result.success(list);
    }

    @PostMapping
    public Result<Integer> dispense(@RequestBody List<Integer> ids) {
        if (ids.isEmpty()) return Result.badRequest("未选中处方");
        try {
            dispenseService.dispense(ids);
            return Result.success("发药成功", ids.size());
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}